package cn.t.web.user;

import cn.t.core.controller.BaseController;
import cn.t.dto.user.ResourceInfoDTO;
import cn.t.facade.user.ResourceInfoFacade;
import cn.t.facade.user.UserSessionFacade;
import cn.t.request.ResourceTreeRequest;
import cn.t.vo.user.ResourceTreeVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 资源权限表服务入口
 *
 * @author l
 */
@Slf4j
@RestController
@RequestMapping("resourceInfo")

@AllArgsConstructor
public class ResourceInfoController extends BaseController<ResourceInfoDTO, ResourceInfoFacade> {

    @GetMapping("/tree")
    public List<ResourceTreeVO> getResourceTree() {
        return facade.getResourceTree();
    }


    @PostMapping("/tree")
    public List<ResourceTreeVO> getResourceTree(@RequestBody ResourceTreeRequest request) {

        if (request != null && request.getRoleId() != null && request.getRoleId() >= 1L) {
            return facade.findResourceTreeByRole(request.getRoleId());
        } else if(userSessionFacade.getSession() != null){
            Long userId = userSessionFacade.getSession().getUserId();
            return facade.getMenuTreeByUser(userId, userSessionFacade.getParticipantIds(null));
        }

        return facade.getResourceTree();
    }


    @GetMapping("/resourceCode")
    public List<String> allResourceCode() {
        return facade.getResourceCode();
    }


    @PostMapping("save")
    @Override
    public Boolean save(@Valid @RequestBody ResourceInfoDTO dto) {
        return facade.add(dto);
    }


    @Autowired
    private UserSessionFacade userSessionFacade;
}

